package view.ui
{
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	import model.ReaderModel;
	
	public class PageCont extends Sprite
	{
		public var pid:int;
		
		public var loaded:Boolean = false;
		
		private var _target:DisplayObject;
		
		private var _loadingAnim:SKloadingAnim;
		
		private var _t_failed:TextField;
		
		private var _tw:Number;
		
		private var _th:Number;
		
		private var _loadTimer:Timer;
		
		private var _hasCalledToLoad:Boolean = false;
		
		private var _m:ReaderModel = ReaderModel.getInstance();
		
		private var _viewW:Number = 0;
		private var _viewH:Number = 0;
		
		private var _scaleValue:Number;
		
		public function PageCont(pid:int, tw:Number, th:Number)
		{
			_tw = tw;
			_th = th;
			_viewW = _tw;
			_viewH = _th;
			this.pid = pid;
			super();
			initUI();
			addEvents();
//			layout();
			
		}
		
		private function layout():void
		{
			if(_loadingAnim == null) return;
//			_loadingAnim.x = 0.5 * _m.sourceScale * _tw;
//			_loadingAnim.y = 0.5 * _m.sourceScale * _th;
			if(_m.viewMode == 0)
			{
				_loadingAnim.x = 0.5 * _viewW;
				_loadingAnim.y = 0.3 * _viewH;
			}else{
				_loadingAnim.x = 0.5 * _viewW;
				_loadingAnim.y = 0.5 * _viewH;
			}
		}
		
		private function addEvents():void
		{			
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);			
			_loadTimer.addEventListener(TimerEvent.TIMER, onLoadTimerComplete);
		}
		
		protected function onLoadTimerComplete(event:TimerEvent):void
		{
			_hasCalledToLoad = true;
			if(_target != null) return;
			_m.loadPage(pid);
		}
		
		protected function onRemoved(event:Event):void
		{
			if(_hasCalledToLoad) return;
			_loadTimer.stop();
			_loadTimer.reset();
		}
		
		protected function onAdded(event:Event):void
		{
			if(_hasCalledToLoad) return;
			_loadTimer.start();
		}
		
		private function initUI():void
		{
			_loadingAnim = new SKloadingAnim();
			_loadingAnim.x = _loadingAnim.y = 200;
			addChild(_loadingAnim);
			_loadTimer = new Timer(500,1);	
		}
		
		public function failedToLoadFile():void
		{
			this.loaded = true;
			_t_failed = new TextField();
			_t_failed.text = _m.style.io_message;
			_t_failed.selectable = false;	
			_t_failed.setTextFormat(new TextFormat("Arial"));
			
			_t_failed.x = 0.5 * _viewW - _t_failed.textWidth * 0.5;
			_t_failed.y = 0.5 * _viewH - _t_failed.textHeight * 0.5;
			
			addChild(_t_failed);
			
			if(_loadingAnim != null && contains(_loadingAnim))
			{
				removeChild(_loadingAnim);
				_loadingAnim = null;
			}
			this.cacheAsBitmap = true;			
			this.loaded = true;
		}

		public function get target():DisplayObject
		{
			return _target;
		}

		public function set target(value:DisplayObject):void
		{
			_target = value;
			addChild(_target);
			if(_loadingAnim != null && contains(_loadingAnim))
			{
				removeChild(_loadingAnim);
				_loadingAnim = null;
			}
			this.cacheAsBitmap = true;			
			this.loaded = true;
		}

		public function get tw():Number
		{
			return _tw;
		}

		public function set tw(value:Number):void
		{
			if(value == _tw) return;
			_tw = value;
		}

		public function get th():Number
		{
			return _th;
		}

		public function set th(value:Number):void
		{
			if(value == _th) return;
			_th = value;
			scaleTo(_scaleValue);
		}
		
		public function scaleTo(value:Number, skipEffect:Boolean = false):void
		{
			_scaleValue = value;
			
			if(_target != null)
			{
				var t:Number = 0.2;
				if(skipEffect) t = 0;
				TweenMax.to(_target, t, {scaleX: value, scaleY: value});
			}
			
			_viewW = _tw * value;
			_viewH = _th * value;
			
			if(_t_failed != null)
			{
				_t_failed.x = 0.5 * _viewW - _t_failed.textWidth * 0.5;
				_t_failed.y = 0.5 * _viewH - _t_failed.textHeight * 0.5;
			}	
			
			if(_loadingAnim == null) return;
			
			if(_m.viewMode == 0)
			{
				_loadingAnim.x = 0.5 * _viewW;
				_loadingAnim.y = 0.3 * _viewH;
			}else{
				_loadingAnim.x = 0.5 * _viewW;
				_loadingAnim.y = 0.5 * _viewH;
			}
		}
		
	}
	
}